export const getAllRoutes = async () => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + '/routes/getAllRoutes',
			method: 'GET'
		});
		if (response.statusCode === 200) {
			// console.log(response.data.data);
			return response.data.data;
		} else {
			console.error('获取路线失败，状态码:', response.statusCode);
			uni.showToast({
				title: '获取路线数据失败',
				icon: 'error'
			});
			return [];
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return [];
	}
};

export const toggleActiveStatus = async (routeId, isActive) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl +
				`/routes/toggleActiveStatus?routeId=${routeId}&isActive=${isActive}`,
			method: 'PUT'
		});
		if (response.statusCode === 200) {
			// uni.showToast({
			//     title: '状态更新成功',
			//     icon: 'success'
			// });
			console.log(response.data);
		} else {
			console.error('状态更新失败，状态码:', response.statusCode);
			uni.showToast({
				title: '状态更新失败',
				icon: 'error'
			});
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
	}
};

export const createRoute = async (route) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + '/routes/createRoute',
			method: 'POST',
			data: route
		});
		if (response.statusCode === 200) {
			uni.showToast({
				title: '路线创建成功',
				icon: 'success'
			});
			return response.data.data; // 根据实际返回结构调整
		} else {
			console.error('创建失败，状态码:', response.statusCode);
			uni.showToast({
				title: '路线创建失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};

export const handleFire = async (fireId, fireLevel, personPhoneNumber) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl +
				`/fires/handle?fireId=${fireId}&fireLevel=${fireLevel}&personPhoneNumber=${personPhoneNumber}`,
			method: 'PUT'
		});
		if (response.statusCode === 200) {
			uni.showToast({
				title: '更新成功',
				icon: 'success'
			});
			return true;
		} else {
			console.error('处理火情失败，状态码:', response.statusCode);
			uni.showToast({
				title: '处理火情失败',
				icon: 'error'
			});
			return false;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return false;
	}
};

export const getFireDescription = async (fireId) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + `/fires/getFireDescription?fireId=${fireId}`,
			method: 'GET'
		});
		if (response.statusCode === 200) {
			return response.data.data;
		} else {
			console.error('获取火情描述失败，状态码:', response.statusCode);
			uni.showToast({
				title: '获取火情描述失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};

export const getFireDescriptions = async () => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + `/fires/getFireDescriptions`,
			method: 'GET'
		});
		if (response.statusCode === 200) {
			return response.data.data;
		} else {
			console.error('获取火情描述失败，状态码:', response.statusCode);
			uni.showToast({
				title: '获取火情描述失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};

export const fireHandleRecord = async (fireId) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + `/fires/fireHandleRecord?fireId=${fireId}`,
			method: 'GET'
		});
		if (response.statusCode === 200) {
			return response.data.data;
		} else {
			console.error('获取处理记录失败，状态码:', response.statusCode);
			uni.showToast({
				title: '获取处理记录失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};


export const login = async (personPhoneNumber, password) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl +
				`/user/login?personPhoneNumber=${personPhoneNumber}&password=${password}`,
			method: 'PUT'
		});
		if (response.statusCode === 200) {
			if (response.data.code === 200) {
				return response.data.data;
			} else {
				console.error('登录失败，状态码:', response.statusCode);
				uni.showToast({
					title: response.data.message || '登录失败',
					icon: 'error'
				});
				return null;
			}
		} else {
			console.error('登录失败，状态码:', response.statusCode);
			uni.showToast({
				title: response.data.message || '登录失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};
export const register = async (personPhoneNumber, personName, position, password) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl +
				`/user/register?personPhoneNumber=${personPhoneNumber}&personName=${personName}&position=${position}&password=${password}`,
			method: 'PUT'
		});
		if (response.statusCode === 200) {
			console.log(response.data);
			return response.data.data;
		} else {
			console.error('注册失败，状态码:', response.statusCode);
			uni.showToast({
				title: response.data.message || '注册失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};

export const updateInfo = async (personPhoneNumber, personName, position, password) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl +
				`/user/update?personPhoneNumber=${personPhoneNumber}&personName=${personName}&position=${position}&password=${password}`,
			method: 'PUT'
		});
		console.log(response);
		if (response.statusCode === 200) {
			console.log("00000000");
			return true;
		} else {
			console.error('更新失败，状态码:', response.statusCode);
			uni.showToast({
				title: response.data.message || '更新失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};

export const getInfo = async (personPhoneNumber) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + `/user/info?personPhoneNumber=${personPhoneNumber}`,
			method: 'GET'
		});
		if (response.statusCode === 200) {
			return response.data.data;
		} else {
			console.error('获取用户信息失败，状态码:', response.statusCode);
			uni.showToast({
				title: response.data.message || '获取用户信息失败',
				icon: 'error'
			});
			return null;
		}
	} catch (error) {
		console.error('请求异常:', error);
		uni.showToast({
			title: `请求异常：${error.errMsg || error.message}`,
			icon: 'none',
			duration: 3000
		});
		return null;
	}
};

export const sendSpeedCommand = async (droneId, xSpeed, ySpeed, rotationSpeed) => {
	try {
		const response = await uni.request({
			url: getApp().globalData.rootUrl + `/monitor/putFlyControlData?drone_id=` + String(
				droneId),
			method: 'PUT',
			data: {
				"xSpeed": xSpeed,
				"ySpeed": ySpeed,
				"rotationSpeed": rotationSpeed,
				"timestamp": Date.now()
			},
			header: {
				'Content-Type': 'application/json'
			}
		});
		if (response.statusCode === 200) {
			return true;
		} else {
			console.error('速度指令发送失败，状态码:', response.statusCode);
			return false;
		}
	} catch (error) {
		// 更详细的错误捕获
		console.error('请求异常:', {
			message: error.errMsg || error.message,
			code: error.errno || error.code,
			stack: error.stack
		});
		return false;
	}
};